#!/bin/csh -f
#
# qconvr: Script for converting various restraint files to simple Aqua format
#
# Options (given before arguments; arbitrary order):
# -c value: 'chain' value to be passed to the "convMRTABLE" script.
# -f      : 'fixorig' option of "convMRTABLE" script set to true.
# -b      : the script will exit when it needs a user defined conversion script
#           and cannot find a file with the default value "convUSERxyzrestr"
#           (see below)
#
# First argument: input restraint file
# Second argument (optional): "MR" file - if given, used to convert names
#                             using the "convMRTABLE" script.
# Rest of input is handled interactively.
#
# The script calls various "convABCxyzrestr" scripts, where
# "xyz" denotes the type of data (noe, hb etc.) and
# "ABC" denotes the format (XPLOR, BIOSYM, USER etc.).
#
# Note: these scripts use temporary files "conv.tmp", "conv.tmptmp" and
# "convMRTABLE.tmp".
#
# ===========================================================================
# Ton Rullmann, October 1994. Script series updated: March, 1996
# =========================================================
# Output format: see aqua/doc/restraint_format.txt
#

# JFD adds:
set debug                           = 1

goto begin

#####################
# usage block
#####################
usage:
echo 'usage: qconvr [-b] [-f] [-c value] input_file [mr_file]'
echo '  the options may be given in any order'
echo '-c value: 'chain' value to be passed to the "convMRTABLE" script'
echo '-f      : 'fixorig' option of "convMRTABLE" script set to true'
echo '-b      : the script will exit when it needs a user defined conversion script'
echo '          and cannot find a file with the default value "convUSERxyzrestr"'
exit (1)

#####################
# initialization
#####################
begin:

if ( $#argv == 0 ) goto usage

set maxtyp  = 4
set batch   = 0
set chnval  = 0
set fixorig = 0

#####################
# process the options
#####################

if ( $debug ) echo "Processing options"

options:
switch ( "$1" )
    case "-b":
	set batch = 1
	shift
	breaksw
    case "-f":
	set fixorig = 1
	shift
	breaksw
    case "-c":
	if ( $#argv < 1 ) goto usage
	shift
	set chnval = $1
	shift
	breaksw
    default:
	goto endoptions
endsw
goto options

#########################
# check for input file(s)
#########################
endoptions:
if ( $#argv < 1 ) goto usage

if ( ! -e $1 ) then
    echo '* input file' $1 'does not exist'
    exit (1)
endif

if ( $#argv >= 2 ) then
    if ( ! -e $2 ) then
	echo '* mr file' $2 'does not exist'
	exit (1)
    endif
endif

echo 'Give restraint type:'
echo ' 1 = distances'
echo ' 2 = torsion angles'
set typ = $<
switch ( $typ )
    case 1:
	set RTYP = "distrestr"
	set data = "noe"
    breaksw
    case 2:
	set RTYP = "torsrestr"
	set data = "torsion"
    breaksw
    default:
	echo '* illegal restraint type'
	exit (1)
endsw

echo 'Give file type:'
echo ' 1 = DISGEO'
echo ' 2 = XPLOR'
echo ' 3 = BIOSYM'
echo ' 4 = DISMAN/DIANA'
echo '99 = USER - you will be prompted for the name of a conversion script'
set typ = $<
if ( $typ < 1 || ( $typ > $maxtyp && $typ != 99 ) ) then
    echo '* illegal file type'
    exit (1)
endif

if ( $RTYP == "distrestr" ) then
    echo 'Give data type: noe, hb, ss'
    set data = $<
endif

if ( $typ == 99 ) then
    set script = convUSER$RTYP
    if ( -e $script ) then
	if ( ! $batch ) then
	    echo 'Name of gawk conversion script is set to' $script
	    echo 'enter correct name or <cr>'
	    set new = $<
	    if ( $new != "" ) then
		set script = $new
	    endif
	endif
    else
        echo '* Default conversion script' $script 'does not exist'
	if ( $batch ) exit (1)
        echo 'Give name of gawk conversion script'
        set script = $<
    endif
endif

echo 'Give output file name:'
set file = $<

echo 'Give title:'
set title = ( $< )

switch ( $typ )

case 1:
    gawk -v TITLE="$title" -v DATA="$data" -f $AQUASCRIPTSDIR/convDISGEO$RTYP $1
    set sts = $status
    \mv conv.tmp $file
    if ( $sts ) then
	echo "* ERROR in convDISGEO"$RTYP
	exit (1)
    else
	echo "* ok"
    endif
breaksw

case 2:
    gawk -v TITLE="$title" -v DATA="$data" -f $AQUASCRIPTSDIR/convXPLOR$RTYP $1
    set sts = $status
    \mv conv.tmp $file
    if ( $sts ) then
	echo "* ERROR in convXPLOR"$RTYP
	exit (1)
    else
	echo "* ok"
    endif
breaksw

case 3:
    gawk -v TITLE="$title" -v DATA="$data" -f $AQUASCRIPTSDIR/convBIOSYM$RTYP $1
    set sts = $status
    \mv conv.tmp $file
    if ( $sts ) then
	echo "* ERROR in convBIOSYM"$RTYP
	exit (1)
    else
	echo "* ok"
    endif
breaksw

case 4:
    gawk -v TITLE="$title" -v DATA="$data" -f $AQUASCRIPTSDIR/convDIANA$RTYP $1
    set sts = $status
    \mv conv.tmp $file
    if ( $sts ) then
	echo "* ERROR in convDIANA"$RTYP
	exit (1)
    else
	echo "* ok"
    endif
breaksw

case 99:
    gawk -v TITLE="$title" -v DATA="$data" -f $script $1
    set sts = $status
    \mv conv.tmp $file
    if ( $sts ) then
	echo "* ERROR in "$script
	exit (1)
    else
	echo "* ok"
    endif
breaksw

default:
    echo '* illegal file type'
    exit (1)
endsw

if ( $#argv >= 2 ) then
    echo "* Starting convMRTABLE to convert atom names according to mrfile"
    echo "* using chain value" $chnval
    gawk -v mrfile=$2 -v chain=$chnval -v fixorig=$fixorig -f $AQUASCRIPTSDIR/convMRTABLE $file
    set sts = $status
    \mv convMRTABLE.tmp $file
    cat convMRTABLE.log
    if ( $sts ) then
	echo "* ERROR in convMRTABLE"
	exit (1)
    endif
endif
